package com.snail.readyvr.mvp.presenter;

import com.snail.readyvr.di.qualifier.AddVideo;
import com.snail.readyvr.di.qualifier.HistoryVideo;
import com.snail.readyvr.mvp.contract.AddVideoActivityContract;
import com.snail.readyvr.mvp.model.DaoSession;
import com.snail.readyvr.mvp.model.VideoHistory;
import com.snail.readyvr.mvp.model.VideoHistoryDao;
import com.snail.readyvr.util.SaveVideoHistoryUtils;

import java.util.List;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by MRKING on 2016/11/18.
 */

public class AddVideoActivityPresenter implements AddVideoActivityContract.Presenter {

    private DaoSession mDaoSession;
    private AddVideoActivityContract.View mAddVideoView;
    private AddVideoActivityContract.View mHistoryVideoView;

    @Inject
    public AddVideoActivityPresenter(DaoSession daoSession, @AddVideo AddVideoActivityContract.View addVideoView, @HistoryVideo AddVideoActivityContract.View historyVideoView) {
        mDaoSession = daoSession;
        mAddVideoView = addVideoView;
        mHistoryVideoView = historyVideoView;
    }

    /**
     * Method injection is used here to safely reference {@code this} after the object is created.
     * For more information, see Java Concurrency in Practice.
     */
    @Inject
    void setupListeners() {
        mAddVideoView.setPresenter(this);
        mHistoryVideoView.setPresenter(this);
    }

    @Override
    public void addVideo(VideoHistory videoHistory) {
        SaveVideoHistoryUtils.saveVideo(mDaoSession,videoHistory);
    }

    @Override
    public void findAllVideoHistory(String userName) {
        mDaoSession.getVideoHistoryDao().queryBuilder()
                .where(VideoHistoryDao.Properties.UserName.eq(userName))
                .orderDesc(VideoHistoryDao.Properties.HistoryId)
                .rx()
                .list()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<VideoHistory>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mHistoryVideoView != null)
                            mHistoryVideoView.error(e);
                    }

                    @Override
                    public void onNext(List<VideoHistory> videoHistories) {
                        if (mHistoryVideoView != null)
                            mHistoryVideoView.findAllVideoHistorySuccess(videoHistories);
                    }
                });
    }

    @Override
    public void deleteAllVideos() {
        mDaoSession.getVideoHistoryDao().rx().deleteAll()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<Void>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mHistoryVideoView != null)
                            mHistoryVideoView.error(e);
                    }

                    @Override
                    public void onNext(Void aVoid) {
                        if (mHistoryVideoView != null)
                            mHistoryVideoView.deleteAllVideosSuccess();
                    }
                });
    }

    @Override
    public void start() {

    }
}
